package com.wang.sort;
//partition和随机快排
public class Demo18_PartitionAndQuickSort {
    public static int partition(int[] arr, int L, int R) {
        if(L > R) {
            return -1;
        }
        if(L == R) {
            return L;
        }
        //定义左边界(<区)
        int lessEqual = L - 1;
        int index = L;
        while (index < R) {
            if(arr[index] <= arr[R]) {
                swap(arr, index, ++lessEqual);
            }
            index++;
        }
        swap(arr, ++lessEqual, R);
        return lessEqual;
    }
    //交换
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] =temp;
    }
    //arr[L.....R] 荷兰国旗划分问题，以arr[R]作划分值
    //  < arr[R]  == arr[R]   > arr[R]
    public static int[] netherlandFlag(int[] arr, int L, int R) {
        if(L > R) {
            return new int[] {-1, -1};
        }
        if(L == R) {
            return new int[] {L, R};
        }
        //定义左边界(<区)
        int less = L - 1;
        int more = R;
        int index = L;
        while (index < more) {
            if(arr[index] == arr[R]) {
                index++;
            }
            else if (arr[index] < arr[R]) {
                swap(arr,index++ , ++less );
            }
            else {
                swap(arr, index, --more);
            }

        }
        //while循环执行完后，目标arr[R]在数组最右边
        //< arr[R]  == arr[R]   > arr[R]  arr[R] == num
        //把arr[more] 和 arr[R]交换 (此时的more应该为 >arr[R]的第一个数)
        //L.....less  less + 1.....more - 1  more.......R - 1   R

        // ======> L.....less  less + 1.....more more + 1.......R
        swap(arr, more, R);
        return new int[] {less + 1, more};
    }
    //O(n*n)
    public static void quickSort1(int[] arr) {
            if(arr == null || arr.length < 2) {
                return;
            }
            process1(arr, 0, arr.length - 1);
    }
    //O(n*n)
    public static void quickSort2(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        process2(arr, 0, arr.length - 1);
    }
    //O(n * logn)
    public static void quickSort3(int[] arr) {
        if(arr == null || arr.length < 2) {
            return;
        }
        process3(arr, 0, arr.length - 1);
    }

    public static void process1(int[] arr, int L, int R) {
            if(L >= R) {
                return;
            }
            //L...R partition  arr[R]   [ <= arr[R]     == arr[R]     > arr[R] ]
            int mid = partition(arr, L, R);
            process1(arr, L, mid - 1);
            process1(arr, mid + 1, R);
    }
    //O(n*n)
    public static void process2(int[] arr, int L, int R) {
        if(L >= R) {
            return;
        }
        //L...R partition  arr[R]   [ <= arr[R]     n个 == arr[R]     > arr[R] ]
        //一次把一批等于arr[R]的数放到中间，然后左右部分递归
        int[] equalArea = netherlandFlag(arr, L, R);
        process1(arr, L, equalArea[0] - 1);
        process1(arr, equalArea[0] + 1, R);
    }
    //随机去取一个目标值num，然后与arr[R]交换，继续玩netherlandFlag
    //左右区间的数据规模相近时，情况优；左右区间数据规模差的很远时，情况最差；
    //O(n * logn)
    public static void process3(int[] arr, int L, int R) {
        if(L >= R) {
            return;
        }
        swap(arr, L + (int)(Math.random() * (R - L + 1)), R);
        int[] equalArea = netherlandFlag(arr, L, R);
        process3(arr, L, equalArea[0] - 1);
        process3(arr, equalArea[0] + 1, R);
    }

    //对数器验证
    //for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length ; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random() - (int) (maxValue * Math.random()));
        }
        return arr;
    }

    //for test
    public static int[] copyArray(int[] arr) {
        if(arr == null){
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    //for test
    public static boolean isEqual(int[] arr1,int[] arr2){
        if((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)){
            return false;
        }
        if(arr1 == null && arr2 == null){
            return true;
        }
        if(arr1.length != arr2.length){
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if(arr1[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }

    //for test
    public static void printArray(int[] arr) {
        if(arr == null) {
            return ;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "");
        }
        System.out.println("");
    }

    //for test
    public static void main(String[] args) {
        int testTime = 50000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            int[] arr3 = copyArray(arr1);
            quickSort1(arr1);
            quickSort2(arr2);
            quickSort3(arr3);
            if(!isEqual(arr1,arr2) || !isEqual(arr2,arr3)){
                succeed =false;
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fuck!");
    }
}
